<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>红绿灯</title>
   
</head>
<body>
    <div id="box"></div>
    
    <script>
        var box=document.getElementById('box')
        function hld(){
            this.color=1;
            this.gu();
            this.rui();
        }
        hld.prototype.gu=function(){
            this.dom=document.createElement('img');
            box.appendChild(this.dom);
            this.dom.src='images/'+this.color+'.jpg'
        }
        hld.prototype.rui=function(){
            var that=this;
            this.dom.onclick=function(){
                   that.xiang()
            }
        }
        hld.prototype.xiang=function(){
           
             this.color++;
                if(this.color==4){
                    this.color=1;
                }
              this.dom.src='images/'+this.color+'.jpg';
            }
     
        new hld();
    </script>
</body>
</html>